import jsYaml, { Type } from "js-yaml";

export type YAMLPlanetSchematics = {
  // 名称ID
  nameID: {
    // 中文名称
    zh: string;
  };

  // 周期时间
  cycleTime: number;

  // 引脚数组
  pins: number[];

  // 类型记录，键为数字，值为包含输入标识和数量的对象
  types: Record<
    number,
    {
      // 是否是输入
      isInput: boolean;
      // 数量
      quantity: number;
    }
  >;
};

export type YAMLPlanetSchematicsMap = Record<number, YAMLPlanetSchematics>;
export const YAMLPlanetSchematicsSchema = jsYaml.DEFAULT_SCHEMA.extend([
  new Type("!YAMLPlanetSchematicsMap", {
    kind: "mapping",
    construct: (data: YAMLPlanetSchematics) => {
      data.cycleTime = Number(data.cycleTime);
      data.pins = data.pins.map((p) => Number(p));
      data.cycleTime = Number(data.cycleTime);
      Object.keys(data.types).forEach((key) => {
        const type = data.types[Number(key)];
        type.quantity = Number(type.quantity);
        type.isInput = Boolean(type.isInput);
      });

      return data;
    },
    instanceOf: Object,
    represent: (data: any) => data,
  }),
]);
